using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model; 

namespace PlugNT.Cms.DAL
{
    public class Templet
    {

        private static string currTableName = WebConfig.TablePrefix + "channel";





        public TempletInfo GetTempletInfo(DataRow dr)
        {
            TempletInfo model = new TempletInfo();

            model.id = (int)dr["id"];
            model.clsno = (dr["clsno"] != null) ? dr["clsno"].ToString() : "";
            model.indexfile = (dr["indexfile"] != null) ? dr["indexfile"].ToString() : "";
            model.listfile = (dr["listfile"] != null) ? dr["listfile"].ToString() : "";
            model.showfile = (dr["showfile"] != null) ? dr["showfile"].ToString() : "";
            model.createpath = (dr["createpath"] != null) ? dr["createpath"].ToString() : "";
            model.ismap = (dr["ismap"] != null) ? int.Parse(dr["ismap"].ToString()) : 0;

            return model;
        }






        public TempletInfo GetTempletInfo(int id)
        {
            TempletInfo model = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,clsno,indexfile,listfile,showfile,createpath,ismap from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetTempletInfo(dt.Rows[0]);
            }
            return model;
        }






        public TempletInfo GetTempletInfo(string clsno)
        {
            TempletInfo model = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,clsno,indexfile,listfile,showfile,createpath,ismap from " + currTableName);
            strSql.Append(" where clsno='" + StringHelper.SqlFilter(clsno) + "'");
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetTempletInfo(dt.Rows[0]);
            }
            return model;
        }









        public bool UpdateByClsno(TempletInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");

            strSql.Append("indexfile='" + StringHelper.SqlFilter(model.indexfile) + "',");
            strSql.Append("listfile='" + StringHelper.SqlFilter(model.listfile) + "',");
            strSql.Append("showfile='" + StringHelper.SqlFilter(model.showfile) + "',");
            strSql.Append("ismap=" + model.ismap.ToString() + " ");

            strSql.Append(" where clsno='" + StringHelper.SqlFilter(model.clsno) + "'");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool UpdateById(TempletInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");

            strSql.Append("indexfile='" + StringHelper.SqlFilter(model.indexfile) + "',");
            strSql.Append("listfile='" + StringHelper.SqlFilter(model.listfile) + "',");
            strSql.Append("showfile='" + StringHelper.SqlFilter(model.showfile) + "',");
            strSql.Append("ismap=" + model.ismap.ToString() + " ");

            strSql.Append(" where id=" + model.id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool Delete(string clsno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");

            strSql.Append("indexfile='',");
            strSql.Append("listfile='',");
            strSql.Append("showfile='',");
            strSql.Append("ismap=0 ");

            strSql.Append(" where clsno='" + StringHelper.SqlFilter(clsno) + "'");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool ReplaceTemplet(int id, string indexfile, string listfile, string showfile)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");

            strSql.Append("indexfile='" + StringHelper.SqlFilter(indexfile) + "',");
            strSql.Append("listfile='" + StringHelper.SqlFilter(listfile) + "',");
            strSql.Append("showfile='" + StringHelper.SqlFilter(showfile) + "'");

            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }









        public DataTable GetList(string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,clsno,indexfile,listfile,showfile,createpath,ismap from " + currTableName + " where 1=1 ");
            strSql.Append(andWhere);
            return DbHelper.TabQuery(strSql.ToString());
        }







        public DataTable GetList(string tablePostfix, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            string andClslist = Tables.GetBindclslist(tablePostfix);
            andClslist = (andClslist == "") ? "" : " and clslist like '" + andClslist + "%' ";
            strSql.Append("select id,clsno,indexfile,listfile,showfile,createpath,ismap from " + currTableName + " where 1=1 ");
            strSql.Append(andWhere + andClslist);
            return DbHelper.TabQuery(strSql.ToString());
        }






        public DataTable GetBingList(string  tablePostfix)
        {
            StringBuilder strSql = new StringBuilder();
            string clslist = Tables.GetBindclslist(tablePostfix);
            clslist = (clslist == "") ? "" : " where clslist like '" + clslist + "%' ";
            strSql.Append("select id,clsno,clsname,clslist,clslistlen,indexfile,listfile,showfile,createpath,ismap from  [" + currTableName + "] ");
            strSql.Append(clslist+" Order By clslist Asc");    //Order By clslist Asc,clsorder Asc
            return DbHelper.TabQuery(strSql.ToString());
        }






        public DataTable GetCreateList(string tablePostfix)
        {
            StringBuilder strSql = new StringBuilder();
            string clslist = Tables.GetBindclslist(tablePostfix);
            clslist = (clslist == "") ? "" : " where a.clslist like '" + clslist + "%' ";
            strSql.Append("select count(b.clsno) as tcount,a.clsno from [" + currTableName + "] as a ");
            strSql.Append(" left join [" + Article.currTableName + tablePostfix + "] as b on a.clsno=b.clsno " + clslist + " group by a.clsno,b.clsno");
            return DbHelper.TabQuery(strSql.ToString());
        }




        public string Help()
        {
            return @"mailto:354990393@qq.com";
        }


    }
}
